package com.code.petapp.activity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.code.petapp.R;
import com.code.petapp.adapter.AdoptImgAdapter;
import com.code.petapp.bean.AdoptBean;
import com.code.petapp.databinding.ActivityAdoptBinding;
import com.code.petapp.http.HttpListener;
import com.code.petapp.util.AdoptUtils;
import com.code.petapp.util.FosterUtils;
import com.code.petapp.util.SPCommonUtils;

import java.util.ArrayList;
import java.util.List;

public class AdoptActivity extends BaseActivity implements AdoptImgAdapter.OnItemClickListener {
    ActivityAdoptBinding binding;
    private AdoptImgAdapter adapter;
    private List<AdoptBean> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityAdoptBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        Context context = this;
        //声明RecyclerView
        RecyclerView recyclerView = binding.recView;
        list = new ArrayList<>();
        load();
        adapter=new AdoptImgAdapter(context,list);
        adapter.setOnItemClickListener(this);
        LinearLayoutManager manager = new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
    }
    private void load(){
        AdoptUtils.selectAll(SPCommonUtils.getInstance().getToken(), new HttpListener<>() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void success(ArrayList<AdoptBean> beanList) {
                list.clear();
                list.addAll(beanList);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void error(int code, String error) {
                toastShortShow(error);
            }
        });
    }

    @Override
    public void onClick(View parent, int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(AdoptActivity.this);
        builder.setIcon(R.drawable.icon);
        builder.setTitle("确定归还？");
        builder.setMessage("可爱的小动物要离开你了。。。");
        builder.setPositiveButton("归还",
                        (DialogInterface dialog, int arg1) -> {
                            dialog.dismiss();
                            AdoptBean adoptBean=new AdoptBean();
                            adoptBean.setId(list.get(position).getId());
                            adoptBean.setStatus("已归还");
                            adoptBean.setAnimalId(list.get(position).getAnimalId());
                            AdoptUtils.update(SPCommonUtils.getInstance().getToken(),adoptBean, new HttpListener<>() {
                                @SuppressLint("NotifyDataSetChanged")
                                @Override
                                public void success(String data) {
                                    toastShortShow("归还成功，我们将为它寻找新主人");
                                    load();
                                    adapter.notifyDataSetChanged();
                                }

                                @Override
                                public void error(int code, String error) {
                                    toastShortShow(error);
                                }
                            });
                        })
                .setNegativeButton("取消",
                        (DialogInterface dialog, int arg1) -> {
                            dialog.dismiss();
                        });
        builder.create().show();
    }
}